﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace System
{
    public class BaseDAL<T> : BaseDal, IBaseDAL<T> where T : BaseModel, new()
    {
        private string _ModelAssemblyName;
        private string _ModelName;

        /// <summary>
        /// 
        /// </summary>
        public BaseDAL()
        { }

        /// <summary>
        /// BaseDAL
        /// </summary>
        /// <param name="configName">数据连接在配置文件中的名称</param>
        public BaseDAL(string configName)
            : base(configName)
        {

        }

        #region 属性

        /// <summary>
        /// 相关对象类型
        /// </summary>
        public override Type ModelType
        {
            get
            {
                if (_ModelType == null)
                {
                    _ModelType = typeof(T);
                }
                return _ModelType;
            }
        }

        /// <summary>
        /// 对象所在程序集名
        /// </summary>
        public override string ModelAssemblyName
        {
            get
            {
                if (string.IsNullOrEmpty(_ModelAssemblyName))
                    _ModelAssemblyName = ModelType.Namespace;
                return _ModelAssemblyName;
            }
            set { }
        }

        /// <summary>
        /// 对象名
        /// </summary>
        public override string ModelName
        {
            get
            {
                if (string.IsNullOrEmpty(_ModelName))
                    _ModelName = ModelType.Name;
                return _ModelName;
            }
            set { }
        }

        /// <summary>
        /// 对应对象
        /// </summary>
        public override BaseModel BaseModel
        {
            get
            {
                if (_BaseModel == null)
                {
                    _BaseModel = new T();
                }
                return _BaseModel;
            }
        }

        #endregion

        #region IDT<T> 成员

        /// <summary>
        /// 新增对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Add(T model, string fields)
        {
            return _Add<T>(model, fields);
        }

        /// <summary>
        /// 新增对象集合
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Add(List<T> list, string fields)
        {
            return _Add<T>(list, fields);
        }

        /// <summary>
        /// 新增对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Add(T model, string fields, string tableName)
        {
            return _Add<T>(model, fields, tableName);
        }

        /// <summary>
        /// 新增对象集合
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Add(List<T> list, string fields, string tableName)
        {
            return _Add<T>(list, fields, tableName);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Remove(T model)
        {
            return _Remove<T>(model);
        }

        /// <summary>
        /// 删除对象集合
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Remove(List<T> list)
        {
            return _Remove<T>(list);
        }

        /// <summary>
        /// 删除指定ID的记录
        /// </summary>
        /// <param name="id">对象ID</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Remove(string id)
        {
            return _Remove<T>(id);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Remove(T model, string tableName)
        {
            return _Remove<T>(model, tableName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Remove(List<T> list, string tableName)
        {
            return _Remove<T>(list, tableName);
        }

        /// <summary>
        /// 删除特定ID的对象
        /// </summary>
        /// <param name="id">对象ID</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Remove(string id, string tableName)
        {
            return _Remove<T>(id, tableName);
        }

        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Modify(T model, string fields)
        {
            return _Modify<T>(model, fields);
        }

        /// <summary>
        /// 修改对象集合
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Modify(List<T> list, string fields)
        {
            return _Modify<T>(list, fields);
        }

        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Modify(T model, string fields, string tableName)
        {
            return _Modify<T>(model, fields, tableName);
        }

        /// <summary>
        /// 修改对象集合
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Modify(List<T> list, string fields, string tableName)
        {
            return _Modify<T>(list, fields, tableName);
        }

        /// <summary>
        /// 新增或修改对象，如对象存在则修改，反之则新增
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int AddOrModify(T model, string fields)
        {
            return _AddOrModify<T>(model, fields);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int AddOrModify(List<T> list, string fields)
        {
            return _AddOrModify<T>(list, fields);
        }

        /// <summary>
        /// 新增或修改对象，如对象存在则修改，反之则新增
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int AddOrModify(T model, string fields, string tableName)
        {
            return _AddOrModify<T>(model, fields, tableName);
        }

        /// <summary>
        /// 新增或修改集合中的对象，如对象存在则修改，反之则新增
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int AddOrModify(List<T> list, string fields, string tableName)
        {
            return _AddOrModify<T>(list, fields, tableName);
        }

        /// <summary>
        /// 获取符合条件的对象
        /// </summary>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(string fields, string filter, string sorts, string tableName, List<DBParameter> parameters)
        {
            return _GetModel<T>(fields, filter, sorts, tableName, parameters);
        }

        /// <summary>
        /// 获取指定ID的对象
        /// </summary>
        /// <param name="id">对象ID</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(string id, string fields, string tableName)
        {
            return _GetModel<T>(id, fields, tableName);
        }

        /// <summary>
        /// 获取符合条件的对象
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(List<DBParameter> parameters, string fields, string tableName)
        {
            return _GetModel<T>(parameters, fields, tableName);
        }

        /// <summary>
        /// 获取符合条件的对象
        /// </summary>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            return _GetModel<T>(fields, filter, sorts, parameters);
        }

        /// <summary>
        /// 获取指定ID的对象
        /// </summary>
        /// <param name="id">对象ID</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(string id, string fields)
        {
            return _GetModel<T>(id, fields);
        }
        /// <summary>
        /// 获取指定ID的对象
        /// </summary>
        /// <param name="id">对象ID</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(string id)
        {
            return _GetModel<T>(id);
        }
        /// <summary>
        /// 获取符合条件的对象
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(List<DBParameter> parameters, string fields)
        {
            return _GetModel<T>(parameters, fields);
        }

        /// <summary>
        /// 获取SQL语句所返回的第一条记录的对象
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(string sql, List<DBParameter> parameters)
        {
            return _GetModel<T>(sql, parameters);
        }

        /// <summary>
        /// 获取所有对象
        /// </summary>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList()
        {
            return _GetList<T>();
        }

        /// <summary>
        /// 执行SQL语句，并返回结果
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(string sql, List<DBParameter> parameters)
        {
            return _GetList<T>(sql, parameters);
        }

        /// <summary>
        /// 获取符合条件的对象
        /// </summary>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            return _GetList<T>(fields, filter, sorts, parameters);
        }

        /// <summary>
        /// 以分页形式获取符合条件的对象集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="recordCount">总记录数。为-1时，将从数据库获取该值</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            return _GetList<T>(pageIndex, pageSize, ref recordCount, fields, filter, sorts, parameters);
        }

        /// <summary>
        /// 返回全部对象
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(string tableName)
        {
            return _GetList<T>(tableName);
        }

        /// <summary>
        /// 获取符合条件的对象集合
        /// </summary>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(string fields, string filter, string sorts, string tableName, List<DBParameter> parameters)
        {
            return _GetList<T>(fields, filter, sorts, tableName, parameters);
        }

        /// <summary>
        /// 以分页形式获取符合条件的对象集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="recordCount">总记录数。为-1时，将从数据库获取该值</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, string tableName, List<DBParameter> parameters)
        {
            return _GetList<T>(pageIndex, pageSize, ref recordCount, fields, filter, sorts, tableName, parameters);
        }

        /// <summary>
        /// 通过IDataReader返回对象
        /// </summary>
        /// <param name="dr">IDataReader</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(System.Data.IDataReader dr)
        {
            return _GetModel<T>(dr);
        }

        /// <summary>
        /// 通过DataRow返回对象
        /// </summary>
        /// <param name="dr">IDataReader</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(System.Data.DataRow dr)
        {
            return _GetModel<T>(dr);
        }

        /// <summary>
        /// 通过DataRowView返回对象
        /// </summary>
        /// <param name="dv">DataView</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(System.Data.DataRowView dv)
        {
            return _GetModel<T>(dv);
        }

        /// <summary>
        /// 通过DataRowView返回对象集合
        /// </summary>
        /// <param name="dr">IDataReader</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(System.Data.IDataReader dr)
        {
            return _GetList<T>(dr);
        }

        /// <summary>
        /// 通过DataTable返回对象集合
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(System.Data.DataTable dt)
        {
            return _GetList<T>(dt);
        }

        /// <summary>
        /// 通过DataView返回对象集合
        /// </summary>
        /// <param name="dv">DataView</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(System.Data.DataView dv)
        {
            return _GetList<T>(dv);
        }

        /// <summary>
        /// 通过符合条件的对象
        /// </summary>
        /// <param name="sqlInfo">封装的筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual T GetModel(SqlInfo sqlInfo, List<DBParameter> parameters)
        {
            return _GetModel<T>(sqlInfo, parameters);
        }

        /// <summary>
        /// 返回符合条件的对象集合
        /// </summary>
        /// <param name="sqlInfo">封装的筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(SqlInfo sqlInfo, List<DBParameter> parameters)
        {
            return _GetList<T>(sqlInfo, parameters);
        }

        /// <summary>
        /// 以分页形式返回符合条件的对象集合
        /// </summary>
        /// <param name="pageInfo">封装的分页数据筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual List<T> GetList(PageInfo pageInfo, List<DBParameter> parameters)
        {
            return _GetList<T>(pageInfo, parameters);
        }

        #endregion



        #region IEBaseDAL<T> 成员

        public virtual int Add(Dictionary<string, T> list, string fields)
        {
            return Add(list, fields, TableName);
        }

        public virtual int Add(Dictionary<string, T> list, string fields, string tableName)
        {
            return _Add<T>(list, fields, tableName);
        }

        public virtual int AddOrModify(Dictionary<string, T> list, string fields)
        {
            return AddOrModify(list, fields, this.TableName);
        }

        public virtual int AddOrModify(Dictionary<string, T> list, string fields, string tableName)
        {
            return _AddOrModify<T>(list, fields, tableName);
        }

        public virtual Dictionary<string, T> GetDicList()
        {
            return GetDicList(this.TableName);
        }

        public virtual Dictionary<string, T> GetDicList(string sql, List<DBParameter> parameters)
        {
            return _GetDicList<T>(sql, parameters);
        }

        public virtual Dictionary<string, T> GetDicList(string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            return _GetDicList<T>(fields, filter, sorts, TableName, parameters);
        }

        public virtual Dictionary<string, T> GetDicList(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            return _GetDicList<T>(pageIndex, pageSize, ref recordCount, fields, filter, sorts, this.TableName, parameters);
        }

        public virtual Dictionary<string, T> GetDicList(string tableName)
        {
            return _GetDicList<T>(tableName);
        }

        public virtual Dictionary<string, T> GetDicList(string fields, string filter, string sorts, string tableName, List<DBParameter> parameters)
        {
            return _GetDicList<T>(fields, filter, sorts, tableName, parameters);
        }

        public virtual Dictionary<string, T> GetDicList(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, string tableName, List<DBParameter> parameters)
        {
            return _GetDicList<T>(pageIndex, pageSize, ref recordCount, fields, filter, sorts, tableName, parameters);
        }

        public virtual int Modify(Dictionary<string, T> list, string fields)
        {
            return _Modify<T>(list, fields, TableName);
        }

        public virtual int Modify(Dictionary<string, T> list, string fields, string tableName)
        {
            return _Modify<T>(list, fields, tableName);
        }

        public virtual int Remove(Dictionary<string, T> list)
        {
            return _Remove<T>(list);
        }

        public virtual int Remove(Dictionary<string, T> list, string tableName)
        {
            return _Remove<T>(list, tableName);
        }

        #endregion




        public List<T> GetTop(int count)
        {
            return _GetTop<T>(TableName, count);
        }

        public List<T> GetTop(int count, string sorts)
        {
            return _GetTop<T>(TableName, count, sorts);
        }

        public List<T> GetTop(int count, string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            return _GetTop<T>(TableName, count, fields, filter, sorts, parameters);
        }

        public List<T> GetTop(string tableName, int count)
        {
            return _GetTop<T>(tableName, count);
        }

        public List<T> GetTop(string tableName, int count, string sorts)
        {
            return _GetTop<T>(tableName, count, sorts);
        }

        public List<T> GetTop(string tableName, int count, string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            return _GetTop<T>(tableName, count, fields, filter, sorts, parameters);
        }


        public int InsertOrUpdateBySQL(T model)
        {
            return _InsertOrUpdateBySQL<T>(model);
        }

        public int InsertOrUpdateBySQL(List<T> list)
        {
            return _InsertOrUpdateBySQL<T>(list);
        }

        public int InsertOrUpdateBySQL(T model, string fields)
        {
            return _InsertOrUpdateBySQL<T>(model, fields);
        }

        public int InsertOrUpdateBySQL(List<T> list, string fields)
        {
            return _InsertOrUpdateBySQL<T>(list, fields);
        }

        public int InsertOrUpdateBySQL(T model, string fields, string tableName)
        {
            return _InsertOrUpdateBySQL<T>(model, fields, tableName);
        }
        public int InsertOrUpdateBySQL(List<T> list, string fields, string tableName)
        {
            return _InsertOrUpdateBySQL<T>(list, fields, tableName);
        }
    }
}
